package com.niit.ai.dl.dltrain.web;

import com.niit.ai.dl.dltrain.service.ModelService;
import com.niit.ai.dl.dltrain.web.model.*;
import com.niit.ai.dl.base.BaseRsp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/model")
public class ModelController {
	private static final Logger LOG = LoggerFactory.getLogger(ModelController.class);

	@Autowired
	private ModelService modelService;

	@RequestMapping(value = "/queryList", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ModelListRsp queryList(@RequestBody @Validated ModelListReq req) {
		LOG.info("req = {}", req);
		ModelListRsp rsp = modelService.queryList(req.getCurrentPage(), req.getPageSize());
		LOG.info("rsp = {}", rsp);
		return rsp;
	}

	@RequestMapping(value = "/queryListByType", method = RequestMethod.GET)
	@ResponseBody
	public ModelListRsp queryListByType(@RequestParam("modelTypeId") Integer modelTypeId) {
		LOG.info("req = {}", modelTypeId);
		ModelListRsp rsp = modelService.queryListByType(modelTypeId);
		LOG.info("rsp = {}", rsp);
		return rsp;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public BaseRsp add(@RequestBody @Validated AddModelReq req) {
		LOG.info("req = {}", req);
		BaseRsp rsp = modelService.add(req.getModel());
		LOG.info("rsp = {}", rsp);
		return rsp;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public BaseRsp update(@RequestBody @Validated UpdateModelReq req) {
		LOG.info("req = {}", req);
		BaseRsp rsp = modelService.update(req.getModel());
		LOG.info("rsp = {}", rsp);
		return rsp;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public BaseRsp delete(@RequestBody @Validated DelModelReq req) {
		LOG.info("req = {}", req);
		BaseRsp rsp = modelService.delete(req.getId());
		LOG.info("rsp = {}", rsp);
		return rsp;
	}
}